<%--
  Created by IntelliJ IDEA.
  User: ZhuChunXiao
  Date: 2018/3/13
  Time: 16:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<div id="page-content">
  <div class="row">
    <div class="fixed-fluid">


      <div class="fixed-sm-200 fixed-md-250 pull-sm-left">
        <div class="panel">
          <%--<div class="pad-all bord-btm">--%>
            <%--<button id="btn_add_dict" class="btn btn-block btn-success"><i class="ti-plus"></i> 新增字典</button>--%>
          <%--</div>--%>
          <p class="pad-hor mar-top text-main text-bold">字典列表</p>
          <div id="dict_list" class="list-group bord-no">
            <c:forEach items="${streetList}" var="street" varStatus="index">
              <a href="javascript:void(0);" key="${street.id}" class="list-group-item">
                <i class="fa fa-eye icon-lg icon-fw"></i>${street.name}
              </a>
            </c:forEach>
          </div>
        </div>
      </div>


      <div class="fluid">
        <div id="dictitem_panel" class="panel">
          <div class="panel-body">
            <h1 id="title_dict_selected" class="page-header text-overflow pad-btm">居民区字典配置</h1>
            <input type="hidden" id="dictChooseId">
            <div class="form-inline">
              <div class="row">
                <div class="col-sm-12 table-toolbar-left">
                  <button id="btn_add_dictitem" class="btn btn-default"><i class="ti-plus"></i> 添加字典项</button>
                </div>
              </div>
            </div>

            <div class="table-responsive">
              <table id="dictitem_list" class="table table-striped">
                <thead>
                <tr>
                  <th style="width:80%">字典项名称</th>
                  <th style="width:20%">操作</th>
                </tr>
                </thead>
                <tbody id="residentTbody">

                </tbody>
              </table>
            </div>

          </div>
        </div>
      </div>


    </div>
  </div>
</div>

<script>

  //选择左侧列表
  $("#dict_list a").click(function(){
    var $current_dict_a = $(this);
    $('#dict_list > a.active').removeClass('active');
    $current_dict_a.addClass('active');
    $("#title_dict_selected").text($current_dict_a.text());
    $("#dictChooseId").val($(this).attr("key"));
    resident_reload($(this).attr("key"));
  });

  //添加字典项
  $("#btn_add_dictitem").click(function(){
    if($("#dictChooseId").val()==""){
      __toastr_error("请先选择街道字典列表");
    }else{
      var parentId = $("#dictChooseId").val();
      var text = $("#title_dict_selected").text().trim();
      openDialog(parentId,text,"");
    }
  });

  //加载列表
  function resident_reload(streetId){
    __ajax_get("<%=path%>/com/resident/getResidentList",{parentId:streetId},function(data){
      $("#residentTbody").empty();
      var list = data.data;
      for(var i=0;i<list.length;i++){
        var newTr='<tr>'+
                '<td>'+list[i].name+'</td>'+
                '<td>'+
                '<a href="javascript:void(0);" class="btn btn-default btn-sm" onclick="fn_resident_edit(\''+list[i].id+'\');"><i class="fa fa-lg fa-edit"></i> 编辑</a>'+
                '<a href="javascript:void(0);" class="btn btn-warning btn-sm" onclick="fn_resident_del(\''+list[i].id+'\',\''+list[i].name+'\');"><i class="fa fa-lg fa-ban"></i> 删除</a>'+
                '</td>'+
                '</tr>';
        $("#residentTbody").append(newTr);
      }
    })
  }

  //编辑
  function fn_resident_edit(keyId){
    var parentId = $("#dictChooseId").val();
    var text = $("#title_dict_selected").text().trim();
    openDialog(parentId,text,keyId);
  }

  //删除
  function fn_resident_del(keyId,name){
    __confirm_dialog("删除字典项","确定要删除"+name+"吗？",function(){
      __ajax_get("<%=path%>/com/resident/delete",{keyId:keyId},function(data){
        if(data.state==1){
          __toastr_success(data.message);
          $("#dict_list .active").trigger("click");
        }else{
          __toastr_error("删除失败");
        }
      });
    });
  }

  //打开dialog
  function openDialog(parentId,text,keyId){
    __open_dialog_form('新建系统字典项', '<%=path%>/com/resident/residentDialog?parentId='+parentId+"&text="+text+"&keyId="+keyId, function (dialogRef) {
      var callback = function (data) {
        if (data) {
          dialogRef.close();

          //刷新
          resident_reload($("#dictChooseId").val());
        } else {
          dialogRef.enableButtons();
        }
      };

      if (fn_resident_check_save(callback) === false) {
        dialogRef.enableButtons();
      }
    });
  }

</script>